package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.google.gson.Gson;
import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.extra.UserChatTable;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.dao.notify.ChatContentTable;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.notify.NotificationHelper;
import com.ljkj.cordial.chat.notify.NotificationUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class FriendAddProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        BusinessProtocolMessages.FriendAddProtocol response = (BusinessProtocolMessages.FriendAddProtocol) msg;
        List<BusinessProtocolEntities.RegisteredFriend> friList = response.getRegisteredFriendList();
        List<BusinessProtocolEntities.TouristsFriend> touList = response.getTouristsFriendList();
        BusinessProtocolEntities.RegisteredUser regUser = response.getRegisteredUser();
        BusinessProtocolEntities.TouristsUser tourUser = response.getTouristsUser();
        FriendTable friendTable = null;
        if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_REGISTERED.equals(msgType) && friList != null) {
            for (BusinessProtocolEntities.RegisteredFriend registeredFriend : friList) {
                if (!ChatApp.getInstance().getUserId().equals(registeredFriend.getFriendUserId())) {
                    friendTable = getFriendTable(registeredFriend.getUserType(), registeredFriend.getFriendId(), registeredFriend.getFriendUserId(), registeredFriend.getFriendNickname(), registeredFriend.getFriendAvatar(), registeredFriend.getFriendAlias(), registeredFriend.getOnlineStatus(), registeredFriend.hasSettings(), registeredFriend.getSettings());
                    insert(friendTable);
                    break;
                }
            }

        }
//        if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_TOURISTS.equals(msgType) && touList != null) {
//            for (BusinessProtocolEntities.TouristsFriend touristsFriend : touList) {
//                if (ChatApp.getInstance().getUserId().equals(touristsFriend.getFriendUserId())) {
//                    continue;
//                }
//                FriendTable friendTable = getFriendTable(touristsFriend.getUserType(), touristsFriend.getFriendId(), touristsFriend.getFriendUserId(), touristsFriend.getFriendNickname(), touristsFriend.getFriendAvatar(), touristsFriend.getFriendAlias(), touristsFriend.getOnlineStatus(), touristsFriend.hasSettings(), touristsFriend.getSettings());
//                insert(friendTable);
//            }
//        }
//        FriendTable friendTable = null;
//        if (response.hasTouristsUser() && !ChatApp.getInstance().getUserId().equals(tourUser.getTemporaryId())) {
//            friendTable = getFriendTable(IMSContacts.UserType.TOURISTS_USER, null, tourUser.getTemporaryId(), tourUser.getNickname(), tourUser.getAvatar(), null, null, false, null);
//            insert(friendTable);
//        }
//        if (response.hasRegisteredUser() && !ChatApp.getInstance().getUserId().equals(regUser.getUserId())) {
//            friendTable = getFriendTable(IMSContacts.UserType.REGISTERED_USER, null, regUser.getUserId(), regUser.getNickname(), regUser.getAvatar(), null, null, false, null);
//            insert(friendTable);
//        }

        if (friList != null || touList != null) {
//            NotificationUtils.messageNotify(activity);
            NotificationUtils.notifyFriend(activity, friendTable, NotificationHelper.NTYPE_FRIEND_ADD);
            if (isOnScreen) {
                refresh(activity);
            }
        }
    }

    private FriendTable getFriendTable(String userType, String friendId, String friendUserId, String friendNickname, String friendAvatar, String friendAlias, String onlineStatus, boolean b, BusinessProtocolEntities.Settings settings) {
        FriendTable friendTable = FriendTable.getRowByFriendUserIdNotNull(friendUserId);
        friendTable.setUserType(IMSUtils.userTypeAnalysis(friendUserId));
        friendTable.setFriendUserId(friendUserId);
        friendTable.setFriendNickname(friendNickname);
        friendTable.setFriendAvatar(friendAvatar);

        friendTable.setFriendId(friendId);
        friendTable.setFriendAlias(friendAlias);
        friendTable.setOnlineStatus(onlineStatus);
        if (b) {
            BusinessProtocolEntities.Settings set = settings;
            friendTable.setOfflineMessage(set.getOfflineMessage());
            friendTable.setSnapchatStatus(set.getSnapchatStatus());
            friendTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            friendTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return friendTable;
    }


    @Override
    public void insert(Object... table) {
        super.insert(table);
        try {
            FriendTable friendTable = (FriendTable) table[0];
            if (IMSContacts.UserType.TOURISTS_USER.equals(friendTable.getUserType())) {
                friendTable.setTouristCreateTime(System.currentTimeMillis());
            }
            FriendTable.insert(friendTable);

            ChatContentTable chatTableBean = ChatContentTable.queryRowByTargetIdNotNull(friendTable.getFriendUserId(), Canstants.SINGLE_CHAT);
            chatTableBean.setIsTOP(0);
            chatTableBean.setChatType(Canstants.SINGLE_CHAT);
            chatTableBean.setTargetId(friendTable.getFriendUserId());
            chatTableBean.setTargetName(friendTable.getFriendNickname());
            chatTableBean.setTargetType(friendTable.getUserType());
            chatTableBean.setTargetAvatarUrl(friendTable.getFriendAvatar());
            chatTableBean.setLastMsgType(MsgType.Text);
            chatTableBean.setUpdateTime(System.currentTimeMillis());
            chatTableBean.setLastMsg(String.format(ChatApp.getInstance().getResources().getString(R.string.Apply_Friend), chatTableBean.getTargetName()));
            chatTableBean.setIsRead(0);
            ChatContentTable.insert(chatTableBean);
            MessageEvent event = MessageEvent.cerateUiRefresh(EventConstants.Action.CHAT_REFRESH_AND_FRIEND);
            event.setData(new Gson().toJson(new UserChatTable(chatTableBean.getTargetId(), chatTableBean.getTargetName(), Canstants.SINGLE_CHAT, chatTableBean.getIsSnapchat() == 1 ? true : false, chatTableBean.getLastMsgType(), chatTableBean.getLastMsg(), chatTableBean.getUpdateTime())));
            EventBus.getDefault().post(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
